package sanphone.com.runningschool;


import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import sanphone.com.runningschool.RequestHttp.RequestHttp;


public class ForceOffline {
    private String id;
    String data;
    private Context mContext;

    public ForceOffline(String id,Context context) {
        this.id = id;
        mContext=context;
    }

    public void check() {
        ExecutorService executorService= Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                RequestHttp requestHttp=new RequestHttp(InterfaceLocation.getLocation()+"selectUserStaus?schoolId="+id,mContext);
                try {
                    String response=requestHttp.get();
                    JSONObject json=new JSONObject(response);
                    String data=json.getString("data");
                    Log.e("ForceOffline data ", data );
                    if(data.equals("NO")){
                        ContextWrapper wrapper=new ContextWrapper(mContext);
                        wrapper.sendBroadcast(new Intent("Intent.FORCE_OFFLINE"));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        executorService.shutdown();


    }
}
